package org.rednebula.framework.core.model.domain;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.rednebula.framework.core.IEnvironment;
import org.rednebula.framework.core.model.State;

public final class RootModel extends BasicModel {


	Map<Object,IModel> contains=new LinkedHashMap<Object, IModel>(3);
	
	public RootModel() {
		super();
		this.setState(State.TRANSIENT);
	}

	public RootModel(State state) {
		super(state);
		this.setState(State.TRANSIENT);
	}

	public void push(IModel model){
		getContains().put(model.getIdent(), model);
	}
	
	public void push(String key,IModel model){
		this.getContains().put(key, model);
	}
	
	public <T> T getModel(Object pk){
		return (T)getContains().get(pk);
	}
	
	private Map<Object,IModel> getContains(){
		return contains;
	}
	
	public void clearModels(){
		getContains().clear();
	}
	
	
	
	@Override
	public void create(IEnvironment env) {
		Map<Object,IModel>  models=getContains();
		Iterator<IModel> iters=models.values().iterator();
		while(iters.hasNext()){
			iters.next().create(env);
		}
	}

	@Override
	public void delete(IEnvironment env) {
		Map<Object,IModel>  models=getContains();
		Iterator<IModel> iters=models.values().iterator();
		while(iters.hasNext()){
			iters.next().delete(env);
		}
	}

	@Override
	public void load(IEnvironment env) {
		Map<Object,IModel>  models=getContains();
		Iterator<IModel> iters=models.values().iterator();
		while(iters.hasNext()){
			iters.next().load(env);
		}
	}

	@Override
	public void modify(IEnvironment env) {
		Map<Object,IModel>  models=getContains();
		Iterator<IModel> iters=models.values().iterator();
		while(iters.hasNext()){
			iters.next().modify(env);
		}
	}
	@Override
	public void other(IEnvironment env) {
		Map<Object,IModel>  models=getContains();
		Iterator<IModel> iters=models.values().iterator();
		while(iters.hasNext()){
			iters.next().other(env);
		}
		
	}
}
